/*
 * DiscreteColorCreator.java
 *
 * Created on 16. juni 2008, 22:18
 */
package no.uib.jexpress_modularized.core.visualization.colors.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JColorChooser;

/**
 *
 * @author Bjarte
 */
public class DiscreteColorCreator extends javax.swing.JPanel implements Serializable{

    private Object pressedObject;
    private DefaultListModel mod;

    /**
     * Creates new form DiscreteColorCreator
     */
    public DiscreteColorCreator() {
        setPreferredSize(new Dimension(50, 400));
        initComponents();
        initTable();
    }

    public void initTable() {

        mod = new DefaultListModel();
        mod.addElement(Color.red);
        mod.addElement(Color.blue);

        mod.addElement(Color.CYAN);
        mod.addElement(Color.MAGENTA);
        mod.addElement(Color.black);


        jList1.setModel(mod);
        ColorListRenderer renderer = new ColorListRenderer();
        jList1.setCellRenderer(renderer);
    }

    public void deleteSelected() {

        int[] sel = jList1.getSelectedIndices();
        List<Object> selected = new ArrayList<Object>();
        for (int s : sel) {
            selected.add(mod.elementAt(sel[s]));
        }

        for (Object o : selected) {
            mod.removeElement(o);
        }
    }

    public void insertColor() {
        mod.addElement(pressedObject);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jList1MousePressed(evt);
            }
        });
        jList1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jList1MouseDragged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jList1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MousePressed

        int row = jList1.locationToIndex(evt.getPoint());
        pressedObject = mod.elementAt(row);

    }//GEN-LAST:event_jList1MousePressed

    private void jList1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseDragged

        int row = jList1.locationToIndex(evt.getPoint());
        Object point = mod.elementAt(row);

        int prevRow = mod.indexOf(pressedObject);

        if (prevRow == row) {
            return;
        }

        if (point != pressedObject) {
            //mod.removeElement(pressedObject);
            mod.set(row, pressedObject);
            mod.set(prevRow, point);
        }

    }//GEN-LAST:event_jList1MouseDragged

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        if (evt.getClickCount() != 2) {
            return;
        }

        int row = jList1.locationToIndex(evt.getPoint());
        Object point = mod.elementAt(row);


        Color color = JColorChooser.showDialog(this, "Select Color", (Color) point);
        mod.set(row, color);


    }//GEN-LAST:event_jList1MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
